package cn.ecnu.service.forum.impl;

import cn.ecnu.mapper.forum.CommentMapper;
import cn.ecnu.pojo.forum.Comment;
import cn.ecnu.service.forum.CommentService;
import cn.ecnu.vo.forum.CommentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Override
    public Comment commentAnPost(CommentVo commentVo) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentVo, comment);
        comment.setCommentTime(new Date()); comment.setFlag(0);
        comment.setLikeCount(0);
        commentMapper.insertComment(comment);
        return comment;
    }

    @Override
    public Comment commentAnComment(CommentVo commentVo) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentVo, comment);
        comment.setCommentTime(new Date()); comment.setFlag(1);
        comment.setLikeCount(0);
        commentMapper.insertComment(comment);
        return comment;
    }

    @Override
    public List<Comment> getCommentByPostId(Integer postId) {
        return commentMapper.getCommentByTypeId(postId, 0);
    }

    @Override
    public List<Comment> getCommentByPostIdOrderByHot(Integer postId) {
        return commentMapper.getCommentByTypeIdOrderByHot(postId, 0);
    }

    @Override
    public List<Comment> getCommentByCommentId(Integer commentId) {
        return commentMapper.getCommentByTypeId(commentId, 1);
    }

    @Override
    public List<Comment> getCommentByCommentIdOrderByHot(Integer commentId) {
        return commentMapper.getCommentByTypeIdOrderByHot(commentId, 1);
    }

    @Override
    public int deleteCommentById(Integer commentId) {
        return commentMapper.deleteCommentById(commentId);
    }
}
